package instantHelper.Raceway;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

public class DbHelper extends SQLiteOpenHelper {
	// TAG named for log
	private static final String TAG = DbHelper.class.getSimpleName();

	// CONSTRACTOR IMPLEMENTED
	public DbHelper(Context context) {
		super(context, InstantHelperConstant.DB_NAME, null,
				InstantHelperConstant.DB_VERSION);
		// TODO Auto-generated constructor stub
	}

	@Override
	public void onCreate(SQLiteDatabase db) {
		// CREATE TABLES(PERSON TABLE)
		// request count is defined for a helping request during the application
		// closed
		String sql = "CREATE TABLE " + InstantHelperConstant.DB_PERSON_TABLE
				+ "(" + InstantHelperConstant.DB_COLUMN_ID
				+ " INTEGER PRIMARY KEY,"
				+ InstantHelperConstant.DB_COLUMN_ANDROID_ID + " TEXT,"
				+ InstantHelperConstant.DB_COLUMN_ADDRESS + " TEXT,"
				+ InstantHelperConstant.DB_COLUMN_EMAIL + " TEXT,"
				+ InstantHelperConstant.DB_COLUMN_NAME + " TEXT,"
				+ InstantHelperConstant.DB_COLUMN_PHONE + " TEXT,"
				+ InstantHelperConstant.DB_COLUMN_SURNAME + " TEXT,"
				+ InstantHelperConstant.DB_COLUMN_TC + " TEXT,"
				+ InstantHelperConstant.DB_COLUMN_USER_NICK + " TEXT,"
				+ InstantHelperConstant.DB_COLUMN_USER_PASSWORD + " TEXT"
				+ ");";
		db.execSQL(sql);
		Log.d(TAG, "on create SQL=" + sql);
		sql = "create table " + InstantHelperConstant.DB_DIRECTORY_TABLE + "("
				+ InstantHelperConstant.DB_COLUMN_ID + " INTEGER PRIMARY KEY,"
				+ InstantHelperConstant.DB_DIRECTORY_ISSEND + " BOOLEAN,"
				+ InstantHelperConstant.DB_DIRECTORY_PATH + " TEXT,"
				+ InstantHelperConstant.DB_DIRECTORY_REQUEST_ID + " INTEGER,"
				+ InstantHelperConstant.DB_DIRECTORY_TYPE + " TEXT,"
				+ InstantHelperConstant.DB_DIRECTORY_DATE + " DATE" + ");";
		db.execSQL(sql);
		
	}

	@Override
	public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
		// DB upgrade first delete all data then create db tables

		Log.w(TAG, "Upgrading database from version " + oldVersion + " to "
				+ newVersion + ", which will destroy all old data");
		db.execSQL("DROP TABLE IF EXISTS person");
		db.execSQL("DROP TABLE IF EXISTS settings");
		onCreate(db);

	}

}
